package tagdb.ui;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import tagdb.ui.util.Constants;
import tagdb.ui.util.ExtensionFileFilter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * LoadDialog is used when a processed denovo file is being loaded by the user.
 *
 * @author Thilo Muth
 */
public class LoadDialog extends JDialog {

    /**
     * Parent main frame.
     */
    private MainFrame parent;

    /**
     * The input panel.
     */
    private JPanel inputPnl;

    /**
     * The spectrum files label.
     */
    private JLabel spectrumFileLbl;

    /**
     * Add spectrum button.
     */
    private JButton addSpectrumBtn;

    /**
     * The selected spectrum file.
     */
    private File selectedSpectrumFile;

    /**
     * The selected output file.
     */
    private File selectedOutputFile;

    /**
     * Spectrum file field.
     */
    private JTextField spectrumFileTtf;

    /**
     * Output file field.
     */
    private JTextField outputFileTtf;

    /**
     * Clear spectrum button.
     */
    private JButton clearSpectrumBtn;

    /**
     * Clear output button.
     */
    private JButton clearOutputBtn;

    /**
     * The output file panel.
     */
    private JPanel outputPnl;

    /**
     * Output file label.
     */
    private JLabel outputFileLbl;

    /**
     * Add output button.
     */
    private JButton addOutputBtn;

    /**
     * Ok button.
     */
    private JButton okBtn;

    /**
     * Cancel button.
     */
    private JButton cancelBtn;



    /**
     * Constructor for the load dialog.
     * @param parent The parent frame.
     * @param modal Flag for dialog being modal.
     */
    public LoadDialog(MainFrame parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
    }

    private void initComponents() {
        setTitle("Import Processed Results");
        CellConstraints cc = new CellConstraints();
        Container cp = getContentPane();
        cp.setLayout(new FormLayout("5dlu, p, 5dlu", "5dlu, p, 5dlu, p, 5dlu, p, 5dlu"));

        outputPnl = new JPanel(new FormLayout("5dlu, 45dlu, 5dlu, l:p, 10dlu, p, 5dlu, p, 5dlu", "5dlu, p, 5dlu"));
        outputPnl.setBorder(BorderFactory.createTitledBorder("PepNovo+ Output File"));
        inputPnl = new JPanel(new FormLayout("5dlu, 45dlu, 5dlu, l:p, 10dlu, p, 5dlu, p, 5dlu", "5dlu, p, 5dlu"));
        inputPnl.setBorder(BorderFactory.createTitledBorder("Spectrum Input File"));

        // Spectrum files label.
        spectrumFileLbl = new JLabel("Spectrum File:");
        outputFileLbl = new JLabel("Output File:");

        // Select button for browsing for the MGF file.
        addSpectrumBtn = new JButton("Browse");
        addSpectrumBtn.setToolTipText("Select spectrum file");

        // Select button for browsing for the MGF file.
        addOutputBtn = new JButton("Browse");
        addOutputBtn.setToolTipText("Select Pepnovo+ output file");

        // Clear button for removing selected files
        clearSpectrumBtn = new JButton("Clear");
        clearSpectrumBtn.setToolTipText("Clear spectrum file");
        clearSpectrumBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selectedSpectrumFile = null;
                spectrumFileTtf.setText("No spectrum file selected");
            }
        });

        // Clear button for removing selected files
        clearOutputBtn = new JButton("Clear");
        clearOutputBtn.setToolTipText("Clear output file");
        clearOutputBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selectedSpectrumFile = null;
                outputFileTtf.setText("No output file selected");
            }
        });

        // Spectrum file textfield
        spectrumFileTtf = new JTextField(25);
        spectrumFileTtf.setEditable(false);
        spectrumFileTtf.setMaximumSize(new Dimension(spectrumFileTtf.getMaximumSize().width, spectrumFileTtf.getPreferredSize().height));
        spectrumFileTtf.setText("No spectrum file selected");

        // Output file textfield
        outputFileTtf = new JTextField(25);
        outputFileTtf.setEditable(false);
        outputFileTtf.setMaximumSize(new Dimension(outputFileTtf.getMaximumSize().width, outputFileTtf.getPreferredSize().height));
        outputFileTtf.setText("No output file selected");

        addSpectrumBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // First check whether a file has already been selected.
                // If so, start from that file's parent.
                File startLocation = new File(parent.getJarFilePath() + File.separator + Constants.INPUT_FOLDER);

                JFileChooser fc = new JFileChooser(startLocation);
                fc.setFileFilter(new ExtensionFileFilter("mgf", false));
                fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                fc.setMultiSelectionEnabled(false);
                int result = fc.showOpenDialog(LoadDialog.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    if (fc.getFileFilter().accept(file)) {
                        selectedSpectrumFile = file;
                        spectrumFileTtf.setText(selectedSpectrumFile.getAbsolutePath());
                    }
                }
            }
        });

        addOutputBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // First check whether a file has already been selected.
                // If so, start from that file's parent.
                File startLocation = new File(parent.getJarFilePath() + File.separator + Constants.OUTPUT_FOLDER);

                JFileChooser fc = new JFileChooser(startLocation);
                fc.setFileFilter(new ExtensionFileFilter("out", false));
                fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                fc.setMultiSelectionEnabled(false);
                int result = fc.showOpenDialog(LoadDialog.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    if (fc.getFileFilter().accept(file)) {
                        selectedOutputFile = file;
                        outputFileTtf.setText(selectedOutputFile.getAbsolutePath());
                    }
                }
            }
        });

        inputPnl.add(spectrumFileLbl, cc.xy(2, 2));
        inputPnl.add(spectrumFileTtf, cc.xy(4, 2));
        inputPnl.add(addSpectrumBtn, cc.xy(6, 2));
        inputPnl.add(clearSpectrumBtn, cc.xy(8, 2));

        outputPnl.add(outputFileLbl, cc.xy(2, 2));
        outputPnl.add(outputFileTtf, cc.xy(4, 2));
        outputPnl.add(addOutputBtn, cc.xy(6, 2));
        outputPnl.add(clearOutputBtn, cc.xy(8, 2));

        // Bottom + button panel
        JPanel buttonPnl = new JPanel(new FormLayout("5dlu, p, 5dlu, p, 5dlu", "p"));
        JPanel bottomPnl = new JPanel(new BorderLayout());

        // OK button
        okBtn = new JButton("OK");
        okBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                parent.openOutputFile(outputFileTtf.getText(), spectrumFileTtf.getText());
                dispose();
            }
        });

        // Cancel button - Dispose the dialog.
        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        buttonPnl.add(okBtn, cc.xy(2, 1));
        buttonPnl.add(cancelBtn, cc.xy(4, 1));
        bottomPnl.add(buttonPnl, BorderLayout.EAST);

        // Add the panels to the dialog.
        this.add(outputPnl, cc.xy(2, 2));
        this.add(inputPnl, cc.xy(2, 4));
        this.add(bottomPnl, cc.xy(2, 6));

        pack();
        setLocationRelativeTo(getOwner());
        setResizable(false);
        setVisible(true);
    }


}
